package com.pengtu.gsj.service.impl.dl;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
import org.codehaus.jackson.type.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pengtu.gsj.dao.base.Page;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.dl.BridgeBasicInfoDao;
import com.pengtu.gsj.entity.dl.BridgeBasicInfo;
import com.pengtu.gsj.service.dl.BridgebasicinfoService;
import com.pengtu.gsj.utils.ConfigUtils;
import com.pengtu.gsj.utils.encode.JsonBinder;

/**
 * 
 * 
 * BridgebasicinfoServiceImpl
 * 
 * 2017年4月19日 下午4:26:57
 * 
 * @version 1.0.0
 * 
 */
@Transactional
@Service
public class BridgebasicinfoServiceImpl implements BridgebasicinfoService {

	@Autowired
	private BridgeBasicInfoDao bridgebasicinfoDao;

	@Override
	public List<BridgeBasicInfo> getBridgebasicinfos(QueryFilter queryFilter) {
		return bridgebasicinfoDao.find(queryFilter);
	}

	@Override
	public Page<BridgeBasicInfo> findBridgebasicinfos(
			Page<BridgeBasicInfo> page, QueryFilter queryFilter) {
		return bridgebasicinfoDao.findPage(page, queryFilter);
	}

	@Override
	public BridgeBasicInfo getBridgebasicinfo(String id) {
		return bridgebasicinfoDao.get(id);
	}

	@Override
	public void saveBridgebasicinfo(BridgeBasicInfo bridgebasicinfo) {
		bridgebasicinfoDao.save(bridgebasicinfo);
	}

	@Override
	public void deleteBridgebasicinfo(String id) {
		bridgebasicinfoDao.delete(id);

	}

	/**
	 * 
	 * dockingBridge:
	 * 适用:从设施监测系统中对接桥梁数据
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	@Override
	public void dockingBridge() throws Exception {
		String urlString = ConfigUtils.getConfig("ssjcAddress");
		// 如果从文件中读取的URL地址最后一个字符不是 '\'，则添加'\'    
        if (urlString.charAt(urlString.length() - 1) != '/') {    
        	urlString += "/";    
        } 
        urlString+="dl/bridgebasicinfo!dockingAllBridge.action";
        URL url;
        System.out.println("url:"+urlString);
		StringBuffer st=new StringBuffer();
		url = new URL(urlString);
		URLConnection urlData=url.openConnection();
		urlData.setConnectTimeout(30000);
		BufferedReader reader =new BufferedReader(new InputStreamReader(urlData.getInputStream(),"utf-8"));
		String line=null;
		while((line=reader.readLine())!=null){
			st.append(line);
		}
		JsonBinder jsonBinder = new JsonBinder(Inclusion.ALWAYS);
		List<BridgeBasicInfo> list = jsonBinder.fromJsonList(st.toString(), new TypeReference<List<BridgeBasicInfo>>() {});
		if (list != null) {
			for (BridgeBasicInfo bridgeBasicInfo : list) {
				bridgebasicinfoDao.save(bridgeBasicInfo);
			}
		}
	}
		
	@Override
	public List<BridgeBasicInfo> getAllBridgeBasicInfo() {
		return bridgebasicinfoDao.getAll();
	}

}
